﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using Telerik.Web.UI;
using ud_gSDMS.Code_Service;

namespace ud_gSDMS.timesheet
{
    public partial class list: mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        protected string _pageId;
        private SiteUser siteUser;
        private int currentUserID = 0;

        void Page_Load(object sender, EventArgs e)
        {
            if (SiteUtils.GetCurrentSiteUser()==null)
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }
            else
            {
                siteUser = SiteUtils.GetCurrentSiteUser();
                currentUserID = siteUser.UserId;
                _pageId = pageId.ToString();
                if (!Page.IsPostBack)
                {
                    PopulateLabels();
                    PopulateControls();
                }
            }
        }
        private void PopulateLabels()
        {

        }
        private void PopulateControls()
        {
            datTimeSheet_Date_from.SelectedDate = DateTime.Now.AddDays(-7);
            datTimeSheet_Date_to.SelectedDate = DateTime.Now;
            datTimeSheet_Date_from.MaxDate = DateTime.Now;
            datTimeSheet_Date_to.MaxDate = DateTime.Now;
            LoadControl();
            LoadGrid();
        }
        private void LoadGrid()
        {
            // //cboAccount.Text.Trim() == "" ? null : cboAccount.SelectedValue,
            var data = gSDMS_BL.ud_gSDMS_TIMESHEET_Search(datTimeSheet_Date_from.SelectedDate == null ? null : datTimeSheet_Date_from.SelectedDate.Value.ToShortDateString(),
                                                         datTimeSheet_Date_to.SelectedDate == null ? null : datTimeSheet_Date_to.SelectedDate.Value.ToShortDateString(),
                                                         cbProjectCode.Text.Trim() == "" ? null : cbProjectCode.SelectedValue,
                                                         cboStatus.Text.Trim() == "" ? null : cboStatus.SelectedValue,
                                                         currentUserID.ToString(), cboLeader.SelectedValue);

            if (ViewState["SortExpression"] != null)
            {
                var dv = data.DefaultView;
                dv.Sort = ViewState["SortExpression"].ToString();
                data = dv.Table;
            }
            grdTimeSheet.DataSource = data;
            grdTimeSheet.DataBind();
            ViewState["listTimesheet"] = data;
        }
        private void LoadControl()
        {
            var db = new gSDMS_DataLinq();
            cboStatus.DataValueField = "CAT_TimeSheet_Status_ID";
            cboStatus.DataTextField = "Name";
            cboStatus.DataSource = Library.LINQToDataTable(db, db.ud_gSDMS_CAT_TIMESHEET_STATUS);
            //cboStatus.DataSource = gSDMS_BL.ud_gSDMS_TIMESHEET_Search(null, null, null, null);
            cboStatus.DataBind();
            

            DataTable tb = gSDMS_BL.getComboProjectCode(currentUserID);
            cbProjectCode.DataSource = tb;
            cbProjectCode.DataValueField = "Project_ID";
            cbProjectCode.DataTextField = "Code";
            cbProjectCode.DataBind();

            cboLeader.DataSource = gSDMS_BL.ud_gSDMS_PROJECT_Leader_Tab();
            cboLeader.DataValueField = "UserID";
            cboLeader.DataTextField = "LoginName";
            cboLeader.DataBind();
            cboLeader.Items.Insert(0, new RadComboBoxItem("", ""));
            cboLeader.SelectedValue = "";
        }
        //private void grdTimeSheet_SelectedIndexChanging(object sender, GridViewPageEventArgs e)
        //{

        //}

        private void grdTimeSheet_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdTimeSheet.PageIndex = e.NewPageIndex;
            LoadGrid();
        }
        
        private void grdTimeSheet_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "delete":
                    gSDMS_BL.ud_gSDMS_TIMESHEET_Del(e.CommandArgument.ToString(), true);
                    break;
                case "editTimeSheet":
                    WebUtils.SetupRedirect(this, SiteRoot + "/gSDMSModule/timesheet/addnew.aspx?id=" + id);
                    break;
            }
        }
        void grdTimeSheet_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LoadGrid();
        }
        void grdTimeSheet_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Direction"] == null) ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " ASC") ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " DESC") ViewState["Direction"] = " ASC";
            grdTimeSheet.EditIndex = -1;
            string strSortExp = e.SortExpression + ViewState["Direction"].ToString();
            ViewState["SortExpression"] = strSortExp;

            LoadGrid();
        }

        void btnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            LoadGrid();
        }

        void grdTimeSheet_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (siteUser == null) return;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string txt = ((DataBoundLiteralControl)e.Row.Cells[2].Controls[0]).Text;
                string text = ((DataBoundLiteralControl)e.Row.Cells[8].Controls[0]).Text;
                if ((text != "Approved" && txt == siteUser.LoginName) || gSDMS_BL.mp_User_IsRole(siteUser.UserId, "1|12")) //"12|1|.."
                {
                    e.Row.FindControl("btnEdit").Visible = true;
                    e.Row.FindControl("btnDelete").Visible = true;
                }
                else
                {
                    
                }
            }
        }

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            grdTimeSheet.PageIndexChanging += grdTimeSheet_PageIndexChanging;
            grdTimeSheet.RowCommand += grdTimeSheet_RowCommand;
            grdTimeSheet.RowDeleting += grdTimeSheet_RowDeleting;
            grdTimeSheet.Sorting += grdTimeSheet_Sorting;
            grdTimeSheet.RowDataBound += grdTimeSheet_RowDataBound;
            btnSearch.Click += btnSearch_Click;
            //btnFinish.Click += btnFinish_Click;
            SuppressPageMenu();
        }

        //void btnFinish_Click(object sender, EventArgs e)
        //{
        //    WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
        //}

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}